{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "601f3882",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.io import loadmat\n",
    "from sklearn.cluster import KMeans\n",
    "from skmultilearn.adapt import MLkNN\n",
    "from sklearn.metrics import f1_score\n",
    "from sklearn.metrics import label_ranking_loss\n",
    "from sklearn.metrics import hamming_loss\n",
    "from sklearn.metrics import average_precision_score\n",
    "from sklearn.metrics import coverage_error\n",
    "from sklearn.metrics import zero_one_loss\n",
    "from sklearn.neighbors import kneighbors_graph\n",
    "import sklearn\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "import sklearn.metrics as metrics\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9c909bd",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = loadmat(f'../../../Datasets/Arts.mat')\n",
    "train = data['train']\n",
    "test = data['test']\n",
    "\n",
    "X_test = train[0][0].T\n",
    "Y_test = train[0][1].T\n",
    "Y_test[Y_test == -1] = 0\n",
    "\n",
    "X =  test[0][0].T\n",
    "Y =  test[0][1].T\n",
    "Y[Y == -1] = 0\n",
    "\n",
    "XTX = X.T @ X\n",
    "\n",
    "#Feature\n",
    "n,d = X.shape\n",
    "#label\n",
    "n,c = Y.shape\n",
    "\n",
    "l1  =  0.01\n",
    "l2  =  0.01\n",
    "l3  =  0.01\n",
    "l4  =  0.01\n",
    "\n",
    "sigma = 1\n",
    "nn = 5\n",
    "S= kneighbors_graph(Y.T, nn, mode='distance', include_self=False).toarray()\n",
    "#     S  = np.exp(-dis/(sigma))\n",
    "A = np.diag(np.sum(S, axis=1))\n",
    "\n",
    "eps = np.finfo(np.float16).eps\n",
    "ppn=0\n",
    "\n",
    "W = np.random.rand(d,c)\n",
    "Z = np.random.rand(c,c)\n",
    "\n",
    "t = 100\n",
    "\n",
    "for i in range(t):\n",
    "    \n",
    "    Qn = np.linalg.norm(2 * W,ord=1)\n",
    "    Q  = 1 / np.maximum(Qn,eps)\n",
    "\n",
    "    Z21 = np.linalg.norm(Z, ord=2, axis=1)\n",
    "    Z_21 = 1 / np.maximum(Z21,eps)\n",
    "    DZ_21 = np.diag(Z_21)\n",
    "\n",
    "    Wu = X.T @ Y @ Z\n",
    "    Wd = X.T @ X @ W + l4 * (Q * W)  \n",
    "    W  = W * (Wu / np.maximum(Wd,eps))\n",
    "\n",
    "    Zu = Y.T @ X @ W + l1 * (Y.T @ Y) + l2 * (Y.T @ Y @ Z @ S) \n",
    "    Zd = Y.T @ Y @ Z + l1 * (Y.T @ Y @ Z @ A) + l3 * (DZ_21 @ Z) \n",
    "    Z  = Z * (Zu / np.maximum(Zd,eps))\n",
    "\n",
    "WW = np.linalg.norm(W,axis=1,ord=2)\n",
    "sQ = np.argsort(WW)\n",
    "nosf = int (20 * d / 100)\n",
    "sX = X[:,sQ[d-nosf:]]\n",
    "classifier = MLkNN(k=10)\n",
    "classifier.fit(sX, Y)\n",
    "# predict\n",
    "predictions = classifier.predict(X_test[:,sQ[d-nosf:]]).toarray()\n",
    "scores = classifier.predict_proba(X_test[:,sQ[d-nosf:]]).toarray()\n",
    "MIC = f1_score(Y_test, predictions, average='micro')\n",
    "MAC = f1_score(Y_test, predictions, average='macro')\n",
    "AVP = average_precision_score(Y_test.T,scores.T)\n",
    "HML = hamming_loss(Y_test,predictions)\n",
    "RNL = label_ranking_loss(Y_test,scores)\n",
    "ZER = zero_one_loss(Y_test,predictions)\n",
    "CVE = coverage_error(Y_test,scores)\n",
    "\n",
    "print(  'Micro-F1 :',MIC,'\\n',\n",
    "        'Macro-F1 :',MAC,'\\n',\n",
    "        'Average Precision :',AVP,'\\n',\n",
    "        'Hamming Loss :',HML,'\\n',\n",
    "        'Ranking Loss :',RNL,'\\n',\n",
    "        'Zero-One Loss :',ZER,'\\n',\n",
    "        'Coverage Error:',CVE)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4941159b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
